package com.shaokel.exception;

import com.shaokel.commonutils.ReturnResult;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 少丶客
 * @ClassName exceptionhandler
 * @Description 异常处理类
 * @Date 2020/5/26 20:19
 * @Vesion 1.0
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody  //返回数据
    public ReturnResult error(Exception e){
        e.printStackTrace();
        return ReturnResult.error().message("执行了全局异常处理..");
    }

    //特定异常处理
    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public ReturnResult error(ArithmeticException e){
        e.printStackTrace();
        return ReturnResult.error().message("执行ArithmeticException异常");
    }


    //自定义异常
    @ResponseBody
    @ExceptionHandler(shaokelException.class)
    public ReturnResult error(shaokelException e){
        log.error(e.getMessage());
        e.printStackTrace();
        return ReturnResult.error().code(e.getCode()).message(e.getMsg());
    }

}